/*
 * Copyright (c) 2013-2015 SimaStudio. All rights reserved.
 *
 * This software is the confidential and proprietary information of SimaStudio.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with SimaStudio.
 *
 * SimaStudio MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * SimaStudio SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.asiainfo.integration.xml;

import com.asiainfo.integration.quartz.ScheduleJob;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

/**
 * Usage:
 *
 * @author Charkey
 * @date 4/24/2016
 */
public class Map2XmlUtils {

    public static void scheduleJobMapToXml(String rootName, String elementParentName, Map<Integer, ScheduleJob> dataMap, URL xmlFileUrl) {
        try {
            File file = new File(xmlFileUrl.toURI().getPath());
            Document doc = DocumentHelper.createDocument();
            Element root = doc.addElement(rootName);
            for (ScheduleJob job : dataMap.values()) {
                Element elementParent = root.addElement(elementParentName);
                elementParent.addElement("id").setText(job.getId()+"");
                elementParent.addElement("jobName").setText(job.getJobName());
                elementParent.addElement("jobGroup").setText(job.getJobGroup());
                elementParent.addElement("beanClass").setText(job.getBeanClass());
                elementParent.addElement("beanName").setText(job.getBeanName());
                elementParent.addElement("methodName").setText(job.getMethodName());
                elementParent.addElement("cronExpression").setText(job.getCronExpression());
                elementParent.addElement("isConcurrent").setText(job.getIsConcurrent()+"");
                elementParent.addElement("status").setText(job.getStatus()+"");
                elementParent.addElement("description").setText(job.getDescription());
            }
            //实例化输出格式对象
            OutputFormat format = OutputFormat.createPrettyPrint();
            //设置输出编码
            format.setEncoding("UTF-8");
            //生成XMLWriter对象，构造函数中的参数为需要输出的文件流和格式
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            //开始写入，write方法中包含上面创建的Document对象
            writer.write(doc);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
